package com.traffic.between.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.gxhl.api.core.tools.ToolSecuritySHA;
import com.traffic.between.config.BetweenProperties;

@SuppressWarnings("deprecation")
public class LLWuXianServiceImpl {
	private static Logger logger = Logger.getLogger(LLWuXianServiceImpl.class);
	public static LLWuXianServiceImpl service = new LLWuXianServiceImpl();

	private static String HOST_ORDER_PATH = BetweenProperties.LLWX_ORDER_URL;
	private static String HOST_ORDERSTATUS_PATH = BetweenProperties.LLWX_ORDERSTATUS_URL;
	private static String LLWX_APPKEY = BetweenProperties.LLWX_APPKEY;
	private static String LLWX_SECURITYKEY = BetweenProperties.LLWX_SECURITYKEY;

		
	@SuppressWarnings({ "resource" })
	public String order(String productId, String phoneNo, String cstmOrderNo) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date d = new Date();

		String time = sdf.format(d);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appkey", LLWX_APPKEY);
		params.put("timeStamp", time);
		params.put("phoneNo", phoneNo);
		params.put("productId", productId);
		params.put("cstmOrderNo", cstmOrderNo);
		params.put("callbackUrl", "");

		String paramsSort = ToolSecuritySHA.dataDecrypt(params);
		String sig = ToolSecuritySHA.shaEncrypt(paramsSort + LLWX_SECURITYKEY);

		params.put("sig", sig);

		String json = new Gson().toJson(params);

		logger.info("between- 流量无线流量订购请求时JSON:  " + json);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(HOST_ORDER_PATH);
		String resultJson = "";
		try {
			if (null != json) {
				// 解决中文乱码问题
				StringEntity entity = new StringEntity(json, "utf-8");
				entity.setContentEncoding("utf-8");
				entity.setContentType("application/json");
				method.setEntity(entity);
			}
			HttpResponse result = httpClient.execute(method);
			logger.info("between- 流量无线流量订购请求时的响应statusCode: " + result.getStatusLine().getStatusCode());
			if (result.getStatusLine().getStatusCode() == 200) {
				resultJson = EntityUtils.toString(result.getEntity(), "utf-8");
				logger.info("between- 流量无线流量订购请求时的响应数据是: " + resultJson);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("between- 流量无线流量订购请求时的响应异常: " + e);
		}
		return resultJson;

	}

	@SuppressWarnings({ "resource" })
	public String orderStatus(String cstmOrderNo) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date d = new Date();
		String resultJson = "";

		String time = sdf.format(d);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appkey", LLWX_APPKEY);
		params.put("timeStamp", time);
		params.put("cstmOrderNo", cstmOrderNo);

		String paramsSort = ToolSecuritySHA.dataDecrypt(params);
		String sig = ToolSecuritySHA.shaEncrypt(paramsSort + LLWX_SECURITYKEY);

		params.put("sig", sig);

		String json = new Gson().toJson(params);
		logger.info("between- 流量无线流量订购请求时JSON:  " + json);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost method = new HttpPost(HOST_ORDERSTATUS_PATH);
		try {
			if (null != json) {
				// 解决中文乱码问题
				StringEntity entity = new StringEntity(json, "utf-8");
				entity.setContentEncoding("utf-8");
				entity.setContentType("application/json");
				method.setEntity(entity);
			}
			HttpResponse result = httpClient.execute(method);
			if (result.getStatusLine().getStatusCode() == 200) {
				resultJson = EntityUtils.toString(result.getEntity(), "utf-8");
				logger.info("between- 流量无线流量订购请求状态时的响应数据是: " + resultJson + "    ,cstmOrderNo" + cstmOrderNo);
			}
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("between- 流量无线流量订购请求时的响应异常: " + e);
		}
		return resultJson;

	}
}
